package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zzyl.nursing.domain.NursingProjectPlan;
import com.zzyl.nursing.dto.NursingPlanDto;
import com.zzyl.nursing.dto.NursingProjectPlanDto;
import com.zzyl.nursing.mapper.NursingProjectPlanMapper;
import com.zzyl.nursing.vo.NursingPlanVo;
import com.zzyl.nursing.vo.NursingProjectPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.NursingPlanMapper;
import com.zzyl.nursing.domain.NursingPlan;
import com.zzyl.nursing.service.INursingPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 护理计划Service业务层处理
 * 
 * @author zhangyan
 * @date 2025-05-23
 */
@Service
public class NursingPlanServiceImpl
        extends ServiceImpl<NursingPlanMapper, NursingPlan>
        implements INursingPlanService {

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;
    @Autowired
    private NursingProjectPlanServiceImpl nursingProjectPlanServiceImpl;

    @Override
    public List<NursingPlanVo> getAll() {
        List<NursingPlan> nursingPlans = baseMapper.selectList(new QueryWrapper<>());
        List<NursingPlanVo> nursingPlanVoList = nursingPlans.stream().map(item -> {
            NursingPlanVo nursingPlanVo = new NursingPlanVo();
            nursingPlanVo.setId(Math.toIntExact(item.getId()));
            nursingPlanVo.setSortNo(item.getSortNo());
            nursingPlanVo.setPlanName(item.getPlanName());
            nursingPlanVo.setStatus(item.getStatus());
            nursingPlanVo.setCreateBy(item.getCreateBy());
            nursingPlanVo.setCreateTime(item.getCreateTime());
            return nursingPlanVo;
        }).collect(Collectors.toList());
        return nursingPlanVoList;
    }

    @Override
    public NursingPlanVo getVoById(Long id) {
        NursingPlanVo nursingPlanVo = new NursingPlanVo();


        //获取
        NursingPlan nursingPlan = baseMapper.selectById(id);

        nursingPlanVo.setId(Math.toIntExact(id));
        nursingPlanVo.setSortNo(nursingPlan.getSortNo());
        nursingPlanVo.setPlanName(nursingPlan.getPlanName());
        nursingPlanVo.setStatus(nursingPlan.getStatus());
        nursingPlanVo.setCreateBy(nursingPlan.getCreateBy());
        nursingPlanVo.setCreateTime(nursingPlan.getCreateTime());
        nursingPlanVo.setUpdateBy(nursingPlan.getUpdateBy());
        nursingPlanVo.setUpdateTime(nursingPlan.getUpdateTime());

        //获取
        LambdaQueryWrapper<NursingProjectPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(id > 0,NursingProjectPlan::getPlanId,id);
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.selectList(lambdaQueryWrapper);

        List<NursingProjectPlanVo> nursingProjectPlanVoList = nursingProjectPlans.stream().map(item -> {
            NursingProjectPlanVo nursingProjectPlanVo = new NursingProjectPlanVo();
            nursingProjectPlanVo.setProjectId(item.getProjectId());
            nursingProjectPlanVo.setId(String.valueOf(item.getId()));
            nursingProjectPlanVo.setPlanId(String.valueOf(id));
            nursingProjectPlanVo.setExecuteTime(item.getExecuteTime());
            nursingProjectPlanVo.setExecuteCycle(String.valueOf(item.getExecuteCycle()));
            nursingProjectPlanVo.setExecuteFrequency(String.valueOf(item.getExecuteFrequency()));

            return nursingProjectPlanVo;
        }).collect(Collectors.toList());

        nursingPlanVo.setProjectPlans(nursingProjectPlanVoList);


        return nursingPlanVo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveDto(NursingPlanDto dto) {

        try {
            NursingPlan nursingPlan = new NursingPlan();
            nursingPlan.setSortNo(dto.getSortNo());
            nursingPlan.setPlanName(dto.getPlanName());
            nursingPlan.setStatus(dto.getStatus());
            save(nursingPlan);

            List<NursingProjectPlanDto> nursingProjectPlanDtos = dto.getProjectPlans();
            List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanDtos.stream().map(item -> {
                NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
                nursingProjectPlan.setPlanId(nursingPlan.getId());
                nursingProjectPlan.setProjectId(Long.valueOf(item.getProjectId()));
                nursingProjectPlan.setExecuteTime(item.getExecuteTime());
                nursingProjectPlan.setExecuteCycle(Math.toIntExact(item.getExecuteCycle()));
                nursingProjectPlan.setExecuteFrequency(Math.toIntExact(item.getExecuteFrequency()));
                return nursingProjectPlan;
            }).collect(Collectors.toList());
            nursingProjectPlanServiceImpl.saveBatch(nursingProjectPlans);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateDto(NursingPlanDto dto) {

        try {
            //修改计划
            NursingPlan nursingPlan = new NursingPlan();
            nursingPlan.setId(Long.valueOf(dto.getId()));
            nursingPlan.setSortNo(dto.getSortNo());
            nursingPlan.setPlanName(dto.getPlanName());
            nursingPlan.setStatus(dto.getStatus());
            updateById(nursingPlan);

            //删除计划相关的中间表
            LambdaQueryWrapper<NursingProjectPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(dto.getId() > 0,NursingProjectPlan::getPlanId,dto.getId());
            nursingProjectPlanServiceImpl.remove(lambdaQueryWrapper);

            //添加
            List<NursingProjectPlanDto> nursingProjectPlanDtos = dto.getProjectPlans();
            List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanDtos.stream().map(item -> {
                NursingProjectPlan nursingProjectPlan = new NursingProjectPlan();
                nursingProjectPlan.setPlanId(nursingPlan.getId());
                nursingProjectPlan.setProjectId(Long.valueOf(item.getProjectId()));
                nursingProjectPlan.setExecuteTime(item.getExecuteTime());
                nursingProjectPlan.setExecuteCycle(Math.toIntExact(item.getExecuteCycle()));
                nursingProjectPlan.setExecuteFrequency(Math.toIntExact(item.getExecuteFrequency()));
                return nursingProjectPlan;
            }).collect(Collectors.toList());
            nursingProjectPlanServiceImpl.saveBatch(nursingProjectPlans);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeId(Long id) {

        try {
            //删除主表
            removeById(id);
            //删除中间表
            LambdaQueryWrapper<NursingProjectPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(id > 0,NursingProjectPlan::getPlanId,id);
            nursingProjectPlanServiceImpl.remove(lambdaQueryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }

        return 1;
    }


}

